package com.wkg.springblogdemo.controller;


import com.wkg.springblogdemo.common.exception.BlogException;
import com.wkg.springblogdemo.pojo.request.BlogInfoRequest;
import com.wkg.springblogdemo.pojo.request.UpdateBlogRequtst;
import com.wkg.springblogdemo.pojo.response.BlogDetailResponse;
import com.wkg.springblogdemo.pojo.response.BlogInfoResponse;
import com.wkg.springblogdemo.service.BlogService;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource(name = "blogService")
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList() {
        log.info("获取图书列表...");
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogDetailResponse getBlogDetail(@NotNull(message = "blogId 不能为空") Integer blogId) {
        if (blogId == null) {
            throw new BlogException("blogId 不能为空");
        }

        log.info("获取图书详情,blogId:{}", blogId);
        return blogService.getDetail(blogId);
    }

    @RequestMapping("/add")
    public Boolean addBlog(@Validated @RequestBody BlogInfoRequest blogInfoRequest) {
        log.info("插入博客 blogInfoRequest：{}", blogInfoRequest);
        return blogService.addBlog(blogInfoRequest);
    }

    @RequestMapping("/update")
    public Boolean blogUpdate(@Validated @RequestBody UpdateBlogRequtst updateBlogRequtst) {
        log.info("更新博客内容 updateBlogRequtst:{}", updateBlogRequtst);
        return blogService.blogUpdate(updateBlogRequtst);

    }

    @RequestMapping("/delete")
    public Boolean blogUpdate(Integer blogId) {
        log.info("更新博客内容 blogId:{}", blogId);
        return blogService.deleteUpdate(blogId);

    }
}
